package com.wanglei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglei.constants.SystemConstants;
import com.wanglei.domain.ResponseResult;
import com.wanglei.domain.entity.Comment;
import com.wanglei.domain.vo.CommentVo;
import com.wanglei.domain.vo.PageVo;
import com.wanglei.enums.AppHttpCodeEnum;
import com.wanglei.exception.SystemException;
import com.wanglei.mapper.UserMapper;
import com.wanglei.service.CommentService;
import com.wanglei.mapper.CommentMapper;
import com.wanglei.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author MrWang
* @description 针对表【sg_comment(评论表)】的数据库操作Service实现
* @createDate 2023-07-04 14:24:25
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>()
                .eq(Comment::getType,commentType) //评论类型 文章评论 or 友链评论
                .eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId) //当是文章评论类型的时候加入articleId的查询条件
                .eq(Comment::getRootId,-1);//获取所有第一层的评论列表
        Page<Comment> page = new Page<>(pageNum,pageSize);
        commentMapper.selectPage(page,queryWrapper);
        List<Comment> commentList = page.getRecords();

        //vo第一层的评论列表
        List<CommentVo> commentVoList = toCommontVoList(commentList);

        //vo 每一个第一层评论的子评论列表赋值
        for (CommentVo commentVo : commentVoList) {
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    //根据根评论的id查询所对应的子评论的集合
    private List<CommentVo> getChildren(Long id){

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>()
                .eq(Comment::getRootId,id)
                .orderByDesc(Comment::getCreateTime);
        List<Comment> comments = commentMapper.selectList(queryWrapper);

        List<CommentVo> commentVoList = toCommontVoList(comments);
        return commentVoList;
    }

    //vo 并给某些属性赋值 username toCommentUserName
    private List<CommentVo> toCommontVoList(List<Comment> commentList) {
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
        //遍历vo集合
        commentVoList.stream().map(commentVo -> {
            commentVo.setUsername(userMapper.selectById(commentVo.getCreateBy()).getNickName());
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if(commentVo.getToCommentUserId() != -1){
                commentVo.setToCommentUserName(userMapper.selectById(commentVo.getToCommentUserId()).getNickName());
            }
            return commentVo;
        }).collect(Collectors.toList());

        return commentVoList;
    }

    //发表评论
    @Override
    public ResponseResult addComment(Comment comment) {
        //评论不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        System.out.println("Comment.................."+comment);
        //评论id creatBy updateBy
        save(comment);
        return ResponseResult.okResult();
    }
}




